package com.frank.oss.controller;

import com.frank.common.entity.Result;
import com.frank.oss.service.FileService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/1 22:15
 * @description
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @GetMapping("/getAllBuckets")
    public String test() {
        try {
            List<String> buckets = fileService.listBuckets();
            return "buckets: " + buckets;
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
          return Result.ok(fileService.uploadFile(uploadFile, bucket, objectName));
    }

}
